package de.dtools.n2g.note;

import de.schlichtherle.io.*;
import de.schlichtherle.io.File;
import de.schlichtherle.io.FileInputStream;
import de.schlichtherle.io.FileOutputStream;
import java.io.*;
import java.nio.channels.FileChannel;

import javax.swing.JTextPane;
import javax.swing.text.Document;

import com.Ostermiller.Syntax.HighlightedDocument;



public class JSDViewer {

	private File jarFile;
	
	private Document doc;
	private EntrySet entrySet;
	
	private FileOutputStream fstrm;
	private ObjectOutput ostrm;
	
	
	public JSDViewer(){
		super();
	}
	
	public JSDViewer(Document doc, EntrySet entrySet){
		super();
		this.doc = doc;
		this.entrySet = entrySet;
		buildXML();
	}
	
	public void createJAR(String fileString, String ext){
		
		
		try {
			File file = new File(fileString);
			int i = fileString.lastIndexOf(java.io.File.separator);
			String dir = fileString.substring(0, i+1);
			
			if(ext.matches("jar")){
				jarFile = new File(getClass().getClassLoader().getResource(
				"jsdviewer.jar").toURI());
				
				java.io.FileOutputStream fstrm = new java.io.FileOutputStream(file);
				FileChannel in = (new java.io.FileInputStream(jarFile)).getChannel();
				FileChannel out = fstrm.getChannel();
	  	      	in.transferTo(0, jarFile.length(), out);
	  	      	in.close();
		      	out.close();
			}else{
				file.mkdir();
			}
			
			File.umount(file);
			
  	      	File temp =  new File(dir + "document.jsd");
			FileOutputStream fstrm2 = new FileOutputStream(temp.getCanonicalPath());
			ostrm = new ObjectOutputStream(fstrm2);
			ostrm.writeObject(doc);
			ostrm.flush();
			ostrm.close();
			
			File.umount();
			
			File docFile = new File(file.getCanonicalPath() + 
					java.io.File.separator+ "document.jsd");
			
			File.cp_p(temp, docFile);
			
			File.umount();
			
			temp.delete();
			
			File.umount(file);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
	}
	
	public Document readJAR (String fileString){
		File file = new File(fileString + java.io.File.separator+ "document.jsd");
		try {
			FileInputStream fin = new FileInputStream(file);
		    ObjectInputStream istrm = new ObjectInputStream(fin);
		    doc = (Document) istrm.readObject();
		    istrm.close();
		    File.umount(file);
		} catch (Exception e) {}
	    return doc;
	}
	
	private void buildXML(){
		
	}
	
	
}
